(function($, com) {
	// 添加购物车
	com.addcar = function(item) {
		var shopcar = localStorage.getItem('_shopcar') || '{}';
		shopcar = JSON.parse(shopcar);

		shopcar[item.goods_id] = {
			goods_id: item.goods_id,
			goods_name: item.goods_name,
			shop_price: item.shop_price,
			original_img: item.original_img,
		};
		
		localStorage.setItem('_shopcar', JSON.stringify(shopcar));

		return shopcar;

	}
	com.getcar = function() {
		var shopcar = localStorage.getItem('_shopcar') || '[]';
		shopcar = JSON.parse(shopcar);

		return shopcar
	}
	// 删除商品
	com.delcar = function(uid) {
		var shopcar = localStorage.getItem('_shopcar') || '{}';
		shopcar = JSON.parse(shopcar);
		delete shopcar[uid];
		localStorage.setItem('_shopcar', JSON.stringify(shopcar));

	}

	com.removeCar = function() {
		localStorage.removeItem("_shopcar");　　
	}

	com.getUid = function(len) {
		var rdmString = "";
		for(; rdmString.length < len; rdmString += Math.random().toString(36).substr(2));
		return rdmString.substr(0, len);
		//return Math.floor(Math.random() * 100000000 + 10000000).toString();
	};

	com.token = function(len) {
		var token = localStorage.getItem('_legend_token');
		if(!token) {
			token = com.getUid(len);
			localStorage.setItem('_legend_token', token);
		}
		return token;
	};

	com.postUrl = function(url, data, callback) {
		var userinfo = com.getState() || {};
		var _token = userinfo.token;
		data['_token'] = _token;
		mui.post(url, data, function(res) {

			callback(res);

		});

	};

	com.createState = function(info) {
		var state = info;
		state.token = localStorage.getItem('_legend_token');
		com.setState(state);
		return state;
	};

	/**
	 * 获取当前状态
	 **/
	com.getState = function() {
		var stateText = localStorage.getItem('cqxf_state') || "{}";
		var rt = JSON.parse(stateText);
		//ckContact(rt.user_id);
		return rt;
	};
	/**
	 * 设置当前状态
	 **/
	com.setState = function(state) {
		state = state || {};
		localStorage.setItem('cqxf_state', JSON.stringify(state));
	};

	com.out = function() {
		localStorage.removeItem('_legend_token');
		com.setState({});
		localStorage.removeItem('cqxf_state');
		mui.getJSON(apiurl + "Login/logout/", null, function(data) {
			//plus.runtime.restart();
			mui.openWindow({
				id: 'login.html',
				url: '/login.html',
				createNew: true,
				show: {
					aniShow: 'slide-in-right'
				},
			});

		});
	}

	/**
	 * 获取应用本地配置
	 **/
	com.setSettings = function(settings) {
		settings = settings || {};
		localStorage.setItem('$settings', JSON.stringify(settings));
	};

	/**
	 * 设置应用本地配置
	 **/
	com.getSettings = function() {
		var settingsText = localStorage.getItem('$settings') || "{}";
		return JSON.parse(settingsText);
	};

	com.checkPwd = function(v, n) {
		if(v.length < n) {
			return false;
		}
		if(/^\d+$/.test(v)) {
			return false;
		}
		if(/^[a-z]+$/i.test(v)) {
			return false;
		}
		if(!/^[A-Za-z0-9]+$/.test(v)) {
			return false;
		}
		return true;
	}

	/**
	 * @author liuyf 2015-4-30
	 * @description 获取系统信息
	 */
	/*
	com.GetDeviceInfo = function() {
		var device = {
			IMEI: plus.device.imei,
			IMSI: '',
			Model: plus.device.model,
			Vendor: plus.device.vendor,
			UUID: plus.device.uuid,
			Screen: plus.screen.resolutionWidth * plus.screen.scale + 'x' + plus.screen.resolutionHeight * plus.screen.scale + '',
			DPI: plus.screen.dpiX + 'x' + plus.screen.dpiY,
			OS: new Object()
		};
		for(var i = 0; i < plus.device.imsi.length; i++) {
			device.IMSI += plus.device.imsi[i];
		}
		var types = {};
		types[plus.networkinfo.CONNECTION_UNKNOW] = '未知网络';
		types[plus.networkinfo.CONNECTION_NONE] = '未连接网络';
		types[plus.networkinfo.CONNECTION_ETHERNET] = '有线网络';
		types[plus.networkinfo.CONNECTION_WIFI] = 'WiFi网络';
		types[plus.networkinfo.CONNECTION_CELL2G] = '2G蜂窝网络';
		types[plus.networkinfo.CONNECTION_CELL3G] = '3G蜂窝网络';
		types[plus.networkinfo.CONNECTION_CELL4G] = '4G蜂窝网络';
		device.NetworkInfo = types[plus.networkinfo.getCurrentType()];
		device.OS = {
			Language: plus.os.language,
			Version: plus.os.version,
			Name: plus.os.name,
			Vendor: plus.os.vendor
		};
		return device;
	};
	*/

	//将BASE 64保存为文件   第一个参数是文件名, 第二个参数是base64字符串，第三个参数是图片质量1-100，最后一个参数回掉函数
	com.baseImgFile = function(uid, base64, quality, callback) {
		quality = quality || 10;
		callback = callback || $.noop;
		var bitmap = new plus.nativeObj.Bitmap();
		// 从本地加载Bitmap图片 
		bitmap.loadBase64Data(base64, function() {
			//    console.log('加载图片成功'); 
			bitmap.save("_doc/" + uid + ".jpg", {
				overwrite: true,
				quality: quality
			}, function(i) {
				callback(i);
				//    console.log('保存图片成功：'+JSON.stringify(i)); 
			}, function(e) {
				console.log('保存图片失败：' + JSON.stringify(e));
			});
		}, function(e) {
			console.log('加载图片失败：' + JSON.stringify(e));
		});
	}

}(mui, window.app = {}));

var langData = langData || {};
var defaultLan = localStorage.getItem('defaultLan') || 'en';
//翻译完注释掉
//var defaultLan = 'en';
//console.log(JSON.stringify(langData[defaultLan]));
lang = function(txt) {
	if(langData[defaultLan] && langData[defaultLan][txt]) {
		return langData[defaultLan][txt];
	}
	return txt;
}
//console.log(JSON.stringify(defaultLan));
function langInit() {
	var
		el = document.querySelectorAll('[lang]'),
		//el = document.querySelectorAll('*'),
		len = el.length,
		i = 0;
	//console.log(JSON.stringify(el));
	if(len > 0) {
		for(; i < len; i++) {
			var obj = el[i],
				inner = obj.innerText,
				//title = obj.getAttribute('title'),
				placeholder = obj.getAttribute('placeholder'),
				//titleLang = langData[defaultLan][title],
				placeholderLang = lang(placeholder),
				innerLang = lang(inner);
			//console.log(obj);
			if(inner && innerLang) {
				obj.innerText = innerLang;
			}
			/*
			if(title && titleLang) {
				obj.setAttribute('title', titleLang);
			}*/
			if(placeholder && placeholderLang) {
				obj.setAttribute('placeholder', placeholderLang);
			}

		}
	}
}
mui.plusReady(function() {
	if(defaultLan && langData[defaultLan]) {
		langInit();
	}
});